cakephp创建RESTful 调用Logic处理逻辑的结构

结构图

restful struct

新建controller

新建StructController,写入代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
namespace App\Controller;

use App\Controller\AppController;
use App\Model\Logic\ArticlesLogic;

class StructController extends AppController {

    // Get默认访问的action
    //路由:http://localhost:8765/s/struct
    function index() {
        $this->autoRender = false;
        $articles = ArticlesLogic::instance()->getArticles();
        $this->responseOk($articles);
    }

    // Get带参数的Action
    //路由:http://localhost:8765/s/struct/1
    function view($id){
        $this->autoRender = false;
        $articles = ArticlesLogic::instance()->getArticleById($id);
        echo json_encode($articles);
    }

    //PUT action
    //路由:http://localhost:8765/s/struct/2
   function edit($id){
    $this->autoRender = false;
    $data = $this->request->getData();
    $article = ArticlesLogic::instance()->update($id,$data);
    $this->responseOk($article);
   }

     //POST action
   //路由:http://localhost:8765/s/struct
   function add(){
     $this->autoRender = false;
    $data = $this->request->getData();//json_decode 方法的第二个参数,false:object true: array
    $article = ArticlesLogic::instance()->add($data);
    $this->responseOk($article);
   }

//      //delete action
    //路由:http://localhost:8765/s/struct/2
   function delete($id){
    $this->autoRender = false;
    $res = ArticlesLogic::instance()->delete($id);
    $this->responseOk($res);
   }

   //映射执行的Delete Action
   //路由:http://localhost:8765/s/struct/deleteAll
   function deleteAll(){
    $this->autoRender = false;
    $this->responseOk('除所有数据的执行删操作');
   }
}

新建Table

在Model中Table文件夹,在文件夹下新建ArticlesTable.php文件,写入代码:

1
2
3
4
5
6
<?php

use Cake\ORM\Table;

class ArticlesTable extends Table {
}

新建Entity

在Model中Entity文件夹,在文件夹下新建Article.php文件,写入代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<?php

namespace App\Model\Entity;

use Cake\ORM\Entity;

class Article extends Entity {

    protected $_accessible = [
        'id' => true,
    ];
}

新建Logic

在Model中新建Logic文件夹,在文件夹下新建UserLogic.php文件,写入代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace App\Model\Logic;

use App\Model\Entity\Article;
use Cake\ORM\TableRegistry;
use DateTime;

class ArticlesLogic {
  // create instance.
  public static function instance()
  {
      static $inst = null;
      if ($inst === null) {
          $inst = new ArticlesLogic();
      }
      return $inst;
  }

  //构造方法
  private function __construct()
  {
    $this->articlesTable = TableRegistry::getTableLocator()->get('Articles');
  }

  public function getArticles(){
    $articles = $this->articlesTable->find()
    ->select(['title', 'author', 'price'])
    ->where(['id >=' => 1])
    ->group(['title', 'author', 'price'])
    ->orderDesc('price');
    return $articles;
  }

  public function getArticleById($id){
      $entity =  $this->articlesTable->get($id);
      return $entity;
  }

  public function add($data){
    $article = new Article($data);
    $article->created_time = new DateTime();
    $article->modify_time = new DateTime();
    return $this->articlesTable->save($article);
  }

  public function update($id,$data){
    $article = new Article($data);
    $entity =  $this->articlesTable->get($id);
    if ($entity == null) {
        return 0;
    }
    foreach($article->getVisible() as $property)
    $entity->$property = $article->$property;
    return $this->articlesTable->save($entity);
  }

  public function delete($id){
    $entity = $this->articlesTable->get($id);
    if ($entity == null) {
        return 0;
    }
    $res =  $this->articlesTable->delete($entity);
    return $res;
  }
}

配置路由

在config/route.php中添加代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Router::scope('/s', function (RouteBuilder $routes) {
    $routes->setExtensions(['json']);
    $routes->resources('Struct', [
        'id' => '.*',
        'map' => [
            'deleteAll' => [
                'action' => 'deleteAll',
                'method' => 'DELETE'
            ]
        ]
    ]);
});

配置完成运行,用http测试各个接口,结果如下: get: 默认
restful struct get

带参数 restful struct get

put: restful struct put

post: restful struct post

delete: 普通delete restful struct delete

映射deleteAll restful struct delete